/*
 * Copyright 2008 Federal Emergency Management Agency
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0 
 *    
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 *  
 */
package gov.fema.hazus.bdc.client.general.capacity;

import gov.fema.hazus.bdc.client.common.ui.AbstractForm;
import gov.fema.hazus.bdc.client.common.ui.BuildingUpdate;
import gov.fema.hazus.bdc.client.common.ui.GenericAnswerListBox;
import gov.fema.hazus.bdc.client.common.ui.TextBoxIntegerValueBlurHandler;
import gov.fema.hazus.bdc.client.general.GeneralConstants;
import gov.fema.hazus.bdc.shared.entity.Building;
import gov.fema.hazus.bdc.shared.entity.Capacity;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;

public class CapacityForm extends AbstractForm implements BuildingUpdate {

	// labels
	private Label daytimeOccupants;
	private Label nighttimeOccupants;
	private Label kitchen;
	private Label kitchenCapacity;
	private Label dining;
	private Label diningCapacity;
	private Label sleeping;
	private Label sleepingCapacity;
	private Label hospitalBeds;
	private Label vehicles;
	private Label hazardousMaterials;
	
	// form fields
	private TextBox formDaytimeOccupants = new TextBox();
	private TextBox formNighttimeOccupants = new TextBox();
	private GenericAnswerListBox formKitchen = new GenericAnswerListBox();
	private TextBox formKitchenCapacity = new TextBox();
	private GenericAnswerListBox formDining = new GenericAnswerListBox();
	private TextBox formDiningCapacity = new TextBox();
	private GenericAnswerListBox formSleeping = new GenericAnswerListBox();
	private TextBox formSleepingCapacity = new TextBox();
	private TextBox formHospitalBeds = new TextBox();
	private TextBox formVehicles = new TextBox();
	private GenericAnswerListBox formHazardousMaterials = new GenericAnswerListBox();
	
	// ui components
	private DockPanel docker;
	private Grid table = new Grid(11,2);
	
	// entities
	private Capacity capacity;
	
	// listeners
	CapacityListBoxChangeListener capacityListBoxListener;
	CapacityTextBoxKeyListener capacityTextBoxListener;
	private TextBoxIntegerValueBlurHandler textBoxIntegerValueBlurHandler;
	
	
	private GeneralConstants constants;
	
	public CapacityForm(Building building)
	{
		super(building);
		
		// set up widgets
		docker = new DockPanel();
		
		// set up tab title
		constants = (GeneralConstants)GWT.create(GeneralConstants.class);
		tabTitle = constants.generalCapacityTabTitle();		
		
		daytimeOccupants = new Label(constants.generalCapacityDaytimeOccupants());
		nighttimeOccupants = new Label(constants.generalCapacityNighttimeOccupants());
		kitchen = new Label(constants.generalCapacityKitchen());
		kitchenCapacity = new Label(constants.generalCapacityKitchenCapacity());
		dining = new Label(constants.generalCapacityDining());
		diningCapacity = new Label(constants.generalCapacityDiningCapacity());
		sleeping = new Label(constants.generalCapacitySleeping());
		sleepingCapacity = new Label(constants.generalCapacitySleepingCapacity());
		hospitalBeds = new Label(constants.generalCapacityHospitalBeds());
		vehicles = new Label(constants.generalCapacityVehicles());
		hazardousMaterials = new Label(constants.generalCapacityHazardousMaterials());
		
		// add labels to form
		table.setWidget(0, 0, daytimeOccupants);
		table.setWidget(1, 0, nighttimeOccupants);
		table.setWidget(2, 0, kitchen);
		table.setWidget(3, 0, kitchenCapacity);
		table.setWidget(4, 0, dining);
		table.setWidget(5, 0, diningCapacity);
		table.setWidget(6, 0, sleeping);
		table.setWidget(7, 0, sleepingCapacity);
		table.setWidget(8, 0, hospitalBeds);
		table.setWidget(9, 0, vehicles);
		table.setWidget(10, 0, hazardousMaterials);
		
		// get entity from building, set up form fields
		capacity = building.getCapacity();
		
		// listeners
		capacityListBoxListener = new CapacityListBoxChangeListener(capacity);
		capacityTextBoxListener = new CapacityTextBoxKeyListener(capacity);
		textBoxIntegerValueBlurHandler = new TextBoxIntegerValueBlurHandler();
		
		// load listeners
		loadListeners();
		
		// load forms
		loadForms();
		
		// add fields to table
		table.setWidget(0, 1, formDaytimeOccupants);
		table.setWidget(1, 1, formNighttimeOccupants);
		table.setWidget(2, 1, formKitchen);
		table.setWidget(3, 1, formKitchenCapacity);
		table.setWidget(4, 1, formDining);
		table.setWidget(5, 1, formDiningCapacity);
		table.setWidget(6, 1, formSleeping);
		table.setWidget(7, 1, formSleepingCapacity);
		table.setWidget(8, 1, formHospitalBeds);
		table.setWidget(9, 1, formVehicles);
		table.setWidget(10, 1, formHazardousMaterials);
		
		// add table to docker
		docker.add(table, DockPanel.CENTER);
		
		initWidget(docker);				
	}
	
	public void updateBuilding(Building building) {
		
		// entities
		this.building = building;
		this.capacity = building.getCapacity();
		
		// listeners
		capacityListBoxListener.updateCapacity(capacity);
		capacityTextBoxListener.updateCapacity(capacity);
		
		// reload forms
		loadForms();

	}	

	private void loadListeners()
	{
		formDaytimeOccupants.addFocusListener(capacityTextBoxListener);
		formDaytimeOccupants.addBlurHandler(textBoxIntegerValueBlurHandler);
		
		formNighttimeOccupants.addFocusListener(capacityTextBoxListener);
		formNighttimeOccupants.addBlurHandler(textBoxIntegerValueBlurHandler);
		
		formKitchen.addChangeListener(capacityListBoxListener);
		
		formKitchenCapacity.addFocusListener(capacityTextBoxListener);
		formKitchenCapacity.addBlurHandler(textBoxIntegerValueBlurHandler);
		
		formDining.addChangeListener( capacityListBoxListener );
		
		formDiningCapacity.addFocusListener(capacityTextBoxListener);
		formDiningCapacity.addBlurHandler(textBoxIntegerValueBlurHandler);
		
		formSleeping.addChangeListener(capacityListBoxListener);
		
		formSleepingCapacity.addFocusListener(capacityTextBoxListener);
		formSleepingCapacity.addBlurHandler(textBoxIntegerValueBlurHandler);
		
		formHospitalBeds.addFocusListener(capacityTextBoxListener);
		formHospitalBeds.addBlurHandler(textBoxIntegerValueBlurHandler);
		
		formVehicles.addFocusListener(capacityTextBoxListener);
		formVehicles.addBlurHandler(textBoxIntegerValueBlurHandler);
		
		formHazardousMaterials.addChangeListener(capacityListBoxListener);
	}
	
	private void loadForms()
	{
		// set data, names, listeners for each form element
		// names are used in the listeners
		if( capacity != null )
		{
			String daytimeOccupants = null;
			if (capacity.getNighttimeOccupants() != null )
			{
				daytimeOccupants = capacity.getDaytimeOccupants().toString();
			}		
			formDaytimeOccupants.setText( daytimeOccupants );
			formDaytimeOccupants.setName( Capacity.DAYTIME );
			
			String nighttimeOccupants = null;
			if (capacity.getNighttimeOccupants() != null )
			{
				nighttimeOccupants = capacity.getNighttimeOccupants().toString();
			}
			formNighttimeOccupants.setText( nighttimeOccupants );
			formNighttimeOccupants.setName( Capacity.NIGHTTIME );
			
			formKitchen.setListItem(capacity.getKitchen() );
			formKitchen.setName( Capacity.KITCHEN );
			
			String kitchenCapacity = null;
			if (capacity.getKitchenCapacity() != null )
			{
				kitchenCapacity = capacity.getKitchenCapacity().toString();
			}		
			formKitchenCapacity.setText( kitchenCapacity );
			formKitchenCapacity.setName( Capacity.KITCHENCAPACITY );
			
			formDining.setListItem( capacity.getDining() ); 
			formDining.setName( Capacity.DINING );
			
			String diningCapacity = null;
			if (capacity.getDiningCapacity() != null )
			{
				diningCapacity = capacity.getDiningCapacity().toString();
			}		
			formDiningCapacity.setText( diningCapacity );
			formDiningCapacity.setName( Capacity.DININGCAPACITY );
			
			formSleeping.setListItem( capacity.getSleeping() );
			formSleeping.setName( Capacity.SLEEPING );
			
			String sleepingCapacity = null;
			if (capacity.getSleepingCapacity() != null )
			{
				sleepingCapacity = capacity.getSleepingCapacity().toString();
			}		
			formSleepingCapacity.setText( sleepingCapacity );
			formSleepingCapacity.setName( Capacity.SLEEPINGCAPACITY );
			
			String hospitalBeds = null;
			if (capacity.getHospitalBeds() != null )
			{
				hospitalBeds = capacity.getHospitalBeds().toString();
			}		
			formHospitalBeds.setText( hospitalBeds );
			formHospitalBeds.setName( Capacity.HOSPITALBEDS );
			
			String vehicles = null;
			if (capacity.getVehicles() != null )
			{
				vehicles = capacity.getVehicles().toString();
			}		
			formVehicles.setText( vehicles );
			formVehicles.setName( Capacity.VEHICLES );
			
			formHazardousMaterials.setListItem( capacity.getHazardousMaterials() );
			formHazardousMaterials.setName( Capacity.HAZARDOUSMATERIALS );
		}
		
		if( building != null )
		{
	        if( building.getMode().equalsIgnoreCase(Building.READ_ONLY))
	        {
	            formDaytimeOccupants.setEnabled(false);
	            formNighttimeOccupants.setEnabled(false);
	            formKitchen.setEnabled(false);
	            formKitchenCapacity.setEnabled(false);
	            formDining.setEnabled(false);
	            formDiningCapacity.setEnabled(false);
	            formSleeping.setEnabled(false);
	            formSleepingCapacity.setEnabled(false);
	            formHospitalBeds.setEnabled(false);
	            formVehicles.setEnabled(false);
	            formHazardousMaterials.setEnabled(false);          
	        }
	        else
	        {
	            formDaytimeOccupants.setEnabled(true);
	            formNighttimeOccupants.setEnabled(true);
	            formKitchen.setEnabled(true);
	            formKitchenCapacity.setEnabled(true);
	            formDining.setEnabled(true);
	            formDiningCapacity.setEnabled(true);
	            formSleeping.setEnabled(true);
	            formSleepingCapacity.setEnabled(true);
	            formHospitalBeds.setEnabled(true);
	            formVehicles.setEnabled(true);
	            formHazardousMaterials.setEnabled(true);  
	        }
		}	
	}
	

}
